const http = require('http');
var express = require('express');
const common = require('./module/common')
const fs = require('fs');
var path = require('path')
var url = require('url')
var app = express();

http.createServer(function(request, response){
    //1.获取地址
    let pathName = url.parse(request.url).pathname;
    console.log(pathName)
    pathName=pathName=='/'?'/index.html':pathName;
    //获取后缀名
    let extname = path.extname(pathName);

    //2.通过fs模块读取数据
    if(pathName!='/index'){
        fs.readFile("../src/index.html",async (err,data)=>{
            if(err){
                response.writeHead(404,{'Content-TYpe':'text/html;charset="utf-8"'});
                response.end('这个页面不存在。');
            }
            let mime = await common.getFileMine(extname)
            response.writeHead(200,{'Content-TYpe':''+mime+';charset="utf-8"'});
            response.end(data);

        })
    }

}).listen(8081,'127.0.0.1');
console.log('Server running at http://127.0.0.1:8081/')






